package jt.classic.app.dms.scanner;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jt.classic.system.user.IUser;


/**
 * DMS Ftp 文件队列
 *
 * @author mido
 *
 */
public class DMSFtpFileArray {
    private static DMSFtpFileArray dffa;
    private List list;
    private Map fileMap;

    private DMSFtpFileArray() {
        this.list = new ArrayList();
        this.fileMap = new HashMap();
    }

    public synchronized static DMSFtpFileArray getInstance() {
        if (dffa == null) {
            dffa = new DMSFtpFileArray();
        }

        return dffa;
    }

    /**
     * 将ftp文件插入队列
     * @param file ftp文件
     * @param user 用户
     */
    public synchronized void push(File file, IUser user) {
        Object[] obj = new Object[] { user, file };
        this.list.add(obj);
        this.fileMap.put(user.ilogid() + "_" + file.getName(), "yes");
    }

    /**
     * 队列是否为空
     * @return boolean
     */
    public synchronized boolean isEmpty() {
        return this.list.isEmpty();
    }

    public synchronized boolean contansFile(File file, IUser user) {
        return this.fileMap.containsKey(user.ilogid() + "_" + file.getName());
    }

    /**
     * 获取队列队首对象
     * <p>Object[]{IUser, File}</p>
     * @return Object[]
     */
    public synchronized Object[] get() {
        Object[] temp = null;

        if (!this.list.isEmpty()) {
            temp = (Object[]) this.list.get(0);
        }

        return temp;
    }

    /**
     * 删除队列首对象
     *
     */
    public synchronized void remove() {
        if (!this.list.isEmpty()) {
            this.list.remove(0);
        }
    }

    /**
     * 清理队列散列表
     *
     */
    public synchronized void clearMap() {
        this.fileMap.clear();
    }
}
